package com.yh.csx.crm.core.enums;

import lombok.AllArgsConstructor;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

/**
 * @author Huang Zhaoping
 * @date 2019/4/11
 */
@AllArgsConstructor
public enum PermissionTypeEnum {
    SELF_SUB(1, "自己及下属"), SELF_ORG(2, "本部门"), SPEC_ORG(3, "指定部门"), ALL(4, "全部"),;

    private int code;
    private String name;

    public int getCode() {
        return code;
    }

    public String getName() {
        return name;
    }

    private static Map<Integer, PermissionTypeEnum> cache = new HashMap<>();

    static {
        Stream.of(PermissionTypeEnum.values()).forEach(type -> cache.put(type.code, type));
    }

    public static PermissionTypeEnum resolve(Integer code) {
        if (code == null) return null;
        return cache.get(code);
    }

    public static String resolveName(Integer code) {
        PermissionTypeEnum type = resolve(code);
        return type == null ? "" : type.name;
    }
}
